package com.khalil.kkadmin.ai.rag.knowledgeset.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.khalil.kkadmin.ai.rag.knowledgeset.entity.RagKnowledgeText;
import com.khalil.kkadmin.ai.rag.knowledgeset.repository.RagKnowledgeTextRepository;
import com.khalil.kkadmin.ai.rag.knowledgeset.service.RagKnowledgeTextService;
import com.khalil.kkadmin.infrastructure.BaseService;
import com.khalil.kkadmin.infrastructure.thread.CustomisedTheadPool;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RagKnowledgeTextServiceImpl extends BaseService<RagKnowledgeTextRepository, RagKnowledgeText, Long> implements RagKnowledgeTextService {


    public RagKnowledgeTextServiceImpl(RagKnowledgeTextRepository repository) {
        super(repository);
    }

    @Override
    protected RagKnowledgeText str2CreateEntity(String json) {
        return JSON.parseObject(json, RagKnowledgeText.class);
    }

    @Override
    protected RagKnowledgeText str2UpdateEntity(String json) {
        return JSON.parseObject(json, RagKnowledgeText.class);
    }

    @Override
    public List<RagKnowledgeText> list(String params) {
        JSONObject jsonObject = JSON.parseObject(params);
        Long knowledgeSetId = jsonObject.getLong("knowledgeSetId");
        if (knowledgeSetId == null) {
            return List.of();
        }
        return repository.findByKnowledgeSetId(knowledgeSetId);
    }

    @Override
    public RagKnowledgeText create(String json) {
        RagKnowledgeText knowledgeText = super.create(json);

        // 异步处理split
        CustomisedTheadPool.execute(() -> {

        });

        return knowledgeText;
    }

    @Transactional
    @Override
    public void deleteByRagDocumentId(Long id) {
        repository.deleteByRagDocumentId(id);
    }

    @Override
    public List<RagKnowledgeText> listByRagDocumentId(Long id) {
        return repository.findByRagDocumentId(id);
    }
}
